﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Xunit;

namespace dogd.Tests
{
	public class PersistingTest
	{
		public int Id
		{
			get;
			set;
		}

		public string Name
		{
			get;
			set;
		}
	}

	
	public class XmlPersistenceFacts
	{
		public class TheSaveToMethod
		{
			[Fact]
			public void WillSaveToXmlString()
			{
				PersistingTest p = new PersistingTest() { Name = "Bob", Id = 1000 };

				StringBuilder builder = new StringBuilder();

				using (TextWriter writer = new StringWriter(builder))
				{
					XmlPersistence.SaveTo<PersistingTest>(writer, p);
				}

				Assert.Contains("Bob", builder.ToString());
			}
		}

		public class TheLoadFromMethod
		{
			[Fact]
			public void WillRehydrateClassFromXml()
			{
				PersistingTest p = new PersistingTest() { Name = "Bob", Id = 1000 };

				StringBuilder builder = new StringBuilder();

				using (TextWriter writer = new StringWriter(builder))
				{
					XmlPersistence.SaveTo<PersistingTest>(writer, p);
				}

				string xml = builder.ToString();

				PersistingTest p2 = null;

				using (TextReader reader = new StringReader(xml))
				{
					p2 = XmlPersistence.LoadFrom<PersistingTest>(reader);
				}

				Assert.NotNull(p2);
				Assert.Equal(p.Id, p2.Id);
				Assert.Equal(p.Name, p2.Name);
			}
		}
	}
}
